package com.sam.im.heiqun.uis.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sam.im.heiqun.R;
import com.sam.im.heiqun.constant.Constant;
import com.sam.im.heiqun.entities.FriendHeadEntity;
import com.sam.im.heiqun.entities.ImFriendEntivity;
import com.sam.im.heiqun.entities.MessageEntivity;
import com.sam.im.heiqun.uis.adapters.holder.NewFriendHolder;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.google.gson.Gson;
import com.yuyh.library.utils.GlideUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;
import java.util.Objects;

/**
 * Created by Joe on 2016/12/25.
 */

public class NewFriendAdpter extends RecyclerView.Adapter<NewFriendHolder> implements View.OnClickListener {

    private static final String TAG = NewFriendAdpter.class.getSimpleName();
    private Context context;
    private List<MessageEntivity> mGroupEntivities;
    private NewFriendListClickListener mItemClickListener;

    public NewFriendAdpter(Context context, List<MessageEntivity> groupEntivities) {
        this.context = context;
        this.mGroupEntivities = groupEntivities;
    }

    public NewFriendAdpter(Context context) {
        this.context = context;
    }

    public void refresh(List<MessageEntivity> groupEntivities) {
        this.mGroupEntivities = groupEntivities;
        notifyDataSetChanged();
        int size = mGroupEntivities.size();
        for (int i = 0; i < size; i++) {
            MessageEntivity entivity = mGroupEntivities.get(i);
            entivity.setMessageNum(0);
            entivity.save();
        }
        EventBus.getDefault().post(Constant.BLACK_MESSAGE_REFRESH);
    }

    public interface NewFriendListClickListener {
        void onAgreeNewFriendClick(View view, int position, int type);
    }

    public void setItemClickListener(NewFriendListClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

    @Override
    public NewFriendHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_new_friend, parent, false);
        return new NewFriendHolder(view, mItemClickListener);
    }

    @Override
    public void onBindViewHolder(NewFriendHolder holder, int position) {//这个需要查找用户信息
        MessageEntivity messageEntivity = mGroupEntivities.get(position);

//        if (2 == messageEntivity.getIstop()){//拒绝好友申请
//            holder.agree.setVisibility(View.GONE);
//            holder.unagree.setText(context.getResources().getString(R.string.apply_unagree));//已拒绝
//        }else {
//            holder.agree.setVisibility(View.VISIBLE);

//        }
        int type = messageEntivity.getType();
        holder.content.setVisibility(type == Constant.NOTICE_OF_APPLY_FRIEND
                | type == Constant.MESSAGE_TYPR_APPLY_FRIEND ? View.VISIBLE : View.GONE);
        if (type == Constant.NOTICE_OF_APPLY_FRIEND | type == Constant.MESSAGE_TYPR_APPLY_FRIEND) {
            JSONObject jsonObject = JSON.parseObject(messageEntivity.getContent());
            holder.content.setText(jsonObject.getString("message"));
        }

        holder.unagree.setVisibility(View.GONE);
        if (ToolsUtils.isMyFriendForId(messageEntivity.getDestid())) {
            holder.agree.setText(R.string.already_agree);
            holder.agree.setAlpha(0.5f);
//            holder.unagree.setVisibility(View.GONE);
        } else {
            holder.agree.setText(R.string.agree);
            holder.agree.setAlpha(1.0f);
//            holder.unagree.setVisibility(View.VISIBLE);
        }

        FriendHeadEntity friendHeadEntity = new Gson().fromJson(messageEntivity.getContent(), FriendHeadEntity.class);

        if (friendHeadEntity != null) {
            GlideUtils.loadCircleImage(context, friendHeadEntity.getHeadUrl(), holder.head_image);
            holder.nick.setText(friendHeadEntity.getName());
        } else {
            holder.nick.setText(messageEntivity.getDestid() + "");
        }
        if (messageEntivity.getImgUrl() != null && !messageEntivity.getImgUrl().equals("")) {
            GlideUtils.loadCircleImage(context, messageEntivity.getImgUrl(), holder.head_image);
        }
        holder.agree.setTag(position);
        holder.agree.setOnClickListener(this);
        holder.unagree.setTag(position);
        holder.unagree.setOnClickListener(this);
    }

    @Override
    public int getItemCount() {
        return mGroupEntivities != null ? mGroupEntivities.size() : 0;
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        switch (v.getId()) {
            case R.id.agree:
                mItemClickListener.onAgreeNewFriendClick(v, position, 0);
                break;
            case R.id.unagree:
                mItemClickListener.onAgreeNewFriendClick(v, position, 1);
                break;
        }

    }
}